package com.ita.acmsystem.merchant.servlet.mock;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.acmsystem.merchant.entity.Merchant;
import com.ita.acmsystem.merchant.entity.mock.ProtocalMock;
import com.ita.acmsystem.merchant.util.SimpleJsonUtil;

/**
 * Servlet implementation class MarchentMessageServletMock
 */
public class MarchentMessageServletMock extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MarchentMessageServletMock() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		handle(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		handle(request, response);
	}

	private void handle(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session = request.getSession();
		Merchant merchant = (Merchant) session.getAttribute("user");
		
		if (merchant == null) {
			merchant = new Merchant();
			merchant.setMerchantName("Gary");
			merchant.setMerchantPic("rice.jpg");
		}

		ProtocalMock protocal = new ProtocalMock();
		protocal.setStatus("success");
		protocal.setMessage("");
		protocal.setData(merchant);

		PrintWriter writer = response.getWriter();
		writer.write(SimpleJsonUtil.format(protocal));
		writer.close();
	}
}
